﻿using System;
using System.Data;
using Swirebev.PhotoDownload.Model;
using System.Data.SqlClient;
using Swirebev.PhotoDownload.Common;
using System.Reflection;

namespace Swirebev.PhotoDownload.DAL
{
	public class PhotoDataAccess
	{
		public static bool SavePhotoInfo(PhotoData photo)
        {
			try
            {
                string _connectionString = Settings.Instance.ConnectionString;

				using (SqlConnection connection = new SqlConnection(_connectionString))
				{
					connection.Open();
					SqlCommand command = new SqlCommand();
					command.Connection = connection;
					command.CommandText = "M_mECS_S_Photo_i";
					command.CommandType = System.Data.CommandType.StoredProcedure;

					command.Parameters.Add("@PhotoId", SqlDbType.NVarChar);
					command.Parameters.Add("@StoreId", SqlDbType.NVarChar);
					command.Parameters.Add("@BusinessTypeCd", SqlDbType.NVarChar);
					command.Parameters.Add("@BusinessId", SqlDbType.NVarChar);
					command.Parameters.Add("@DisplayNm", SqlDbType.NVarChar);
                    command.Parameters.Add("@PhysicalNm", SqlDbType.NVarChar);
                    command.Parameters.Add("@CreateAt", SqlDbType.DateTime);

					command.Parameters["@PhotoId"].Value = photo.ObjectId;
					command.Parameters["@StoreId"].Value = photo.StoreId;
					command.Parameters["@BusinessTypeCd"].Value = photo.BusinessTypeCd;//"420001";
					command.Parameters["@BusinessId"].Value = photo.BusinessId;
					command.Parameters["@DisplayNm"].Value = photo.DisplayName;
					command.Parameters["@PhysicalNm"].Value = photo.PhotoName;
                    command.Parameters["@CreateAt"].Value = photo.CreateAt;
					command.ExecuteNonQuery();
                    Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "SQL SERVER:success to save [" + photo.PhotoName + "]");
					return true;
				}
			}
			catch (Exception ex)
			{
				Log.Error(MethodBase.GetCurrentMethod().DeclaringType, "SQL SERVER failed to save photoinfo", ex);
				return false;
			}
		}
	}
}
